package org.zjx.swing.controller;

import lombok.Data;
import org.zjx.swing.model.LoginModel;
import org.zjx.swing.model.MenuModel;
import org.zjx.swing.view.LoginView;
import org.zjx.swing.view.MenuView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Data
public class LoginController {
    private LoginView view;
    private LoginModel model;

    public LoginController(LoginView view, LoginModel model) {
        this.view = view;
        this.model = model;

        // 设置确认按钮的事件监听器
        view.setConfirmButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = view.getUsername();
                String password = view.getPassword();

                // 验证用户名和密码
                if (model.validateLogin(username, password)) {
                    // 登录成功
                    view.clearFields();
                    view.dispose(); // 关闭当前窗口

                    toMenuView();
                } else {
                    // 登录失败
                    view.showErrorMessage("用户名或密码错误！");
                }
            }
        });

        // 设置取消按钮的事件监听器
        view.setCancelButtonListener(e -> System.exit(0)); // 关闭应用程序
    }

    public void toMenuView(){
        MenuModel menuModel = new MenuModel();
        MenuView menuView = new MenuView();
        MenuController menuController = new MenuController(menuModel, menuView);
        menuView.setVisible(true); // 打开新的窗口
    }

}
